<?php

class ChartsMod extends Modbase{
	
	private $type = array('bar','line','pie');
	private $data = array();
	private $handle = null;
	
	
	public function __construct(){
	}

	public function type($type){
		$type = ucfirst($type);
		$this->handle = new $type();

		$this->data['legend'] = array(
		
		//								"orient"=>"horizontal",
		//								"x"=>"right",
		//								"y"=>"top",
		//								"borderWidth"=>"1",
		//								"borderColor"=>"#abcdef",
		//								"padding"=>"10",
		//								"itemGap"=>"10"
		);
		$this->data['tooltip'] = array("trigger"=>"axis");


		$this->data['yAxis'] = array("type"=>'value','splitArea'=>array("show"=>true));

		$this->data['grid'] = array('x'=>'50px','y'=>'50px','x2'=>'30px','y2'=>'30px');
	}

	/**
	 * 去顶是否设定类型，如果未设定类型，需要先设置
	 * 注：也不是一定要先设定，就是逼着遵循固定方式
	 */
	private function typeCheck(){
		if(empty($this->handle)){
			throw new PException("请先执行type(),设定输出图形");
		}
	}

	public function legend($legend){
		$this->typeCheck();
		foreach($legend as $key=>$val){
			if(!is_string($val)){
				throw new PException("图例名字只能是文字文本");
			}
		}
		$this->data['legend']['data'][] = $legend;
	}
	private function xyCheck(){
		if(empty($this->data['xAxis']) && empty($this->data['yAxis'])){
			throw new PException("请先执行x(),y()设定坐标轴");
		}
	}

	public function toolbox(){
		$this->data['toolbox'] = array("show"=>TRUE,
										"feature"=>array("saveAsImage"=>array("show"=>TRUE))
		);

	}

	public function x($data,$type='category'){
		$this->data['xAxis']['type'] = $type;
		$this->data['xAxis']['data'] = $data;
	}
	public function y($data,$type='value'){
		$this->data['yAxis'][] = $data;
	}
	
	//添加 图例数据
	public function data($legend,$type,$data){
		$this->typeCheck();
		$this->xyCheck();
		
		//检查所提交的数据内容是否格式正确
		foreach($data as $key=>$val){
			if(!is_numeric($val)){
				throw new PException("'{$val}'必须为数字");
			}
		}
		$this->data['series'][] = array(
										"name"=>$legend,
										"type"=>$type,
										"data"=>$data
									);
	}
	
	
	public function show($name='option'){
		$this->typeCheck();

		$this->handle->input($this->data);
		$this->handle->init();
		$this->handle->output($name);
	}
	
	
	public function __destruct(){}
}
